#include"options.h"

struct Options options;

const char * argp_program_bug_address="shenhanc@gmail.com";

const char * argp_program_version="procmonitor 0.6.1";

static char program_doc[] = 
  "procmonitor -- use it to:\n";

static char args_doc[] = "procname [PROC_OPTION...]";


#define OPT_KEY_DEBUG 1010
#define OPT_KEY_USERID 1013
#define OPT_KEY_USERNAME 1015
#define OPT_KEY_INTERVAL 1017
#define OPT_KEY_HOGS     1019

static struct argp_option program_options[] = 
  {
    { "user-id", OPT_KEY_USERID, "UESRID", 0,
      "Sepcify user by id. If USERID starts with '@', ids will be read from the file named after this '@'. Defaults to current user" },
    { "user-name", OPT_KEY_USERNAME, "USERNAME", 0,
      "Specify user by name. If USERNAME starts with '@', names will be read from the file named after this '@'. Defaults to current user" },
    { "interval", OPT_KEY_INTERVAL, "INTERVAL", 0, "Specify the sampling interval seconds, defaults to 3" },
    { "report-cpu-hogs", OPT_KEY_HOGS, 0, 0, "Report out hog processes of the user" },
    { "debug", OPT_KEY_DEBUG, 0, 0, "Print debug information about job monitor while running(only for linux)" },
    { 0 }
  };

static int parse_int( int * r, char * str )
{
  char * t = str;
  *r = 0;
  while( *t )
    {
      if( *t >= '0' && *t <= '9' )
        {
          (*r) *= 10;
          (*r) += *t - '0';
        }
      else
        {
          return 1;
        }
      ++t;
    }
  return 0;
}

static error_t parse_opt( int key, char * arg, struct argp_state * state )
{
  char * t;
  int * pint;
  switch( (int)key )
    {
    case OPT_KEY_DEBUG:
      options.debug = 1;
      break;

    case OPT_KEY_USERID:
      options.uid = arg;
      break;
      
    case OPT_KEY_USERNAME:
      options.un = arg;
      break;

    case OPT_KEY_INTERVAL:
      options.interval = strtol( arg, &t, 10 );
      if( *t != 0 )
        {
          fprintf( stdout, "illegal value for --interval, defaults to 3\n" );
          options.interval = 3;
        }
      break;

    case OPT_KEY_HOGS:
      options.report_cpu_hogs = 1;
      break;

    case ARGP_KEY_ERROR:
      argp_usage( state );
      break;

    case ARGP_KEY_ARG:
      return ARGP_ERR_UNKNOWN;

    case ARGP_KEY_ARGS:
      return ARGP_ERR_UNKNOWN;
      
    }
  return 0;
}

static const struct argp argp = { program_options, parse_opt, args_doc, program_doc };

static int check_options()
{
  if( options.uid != NULL && options.un != NULL )
    {
      fprintf( stderr, "You can specify only one of \"user-id\" and -\"user-name\", please use --help.\n" );
      return 1;
    }
  if( options.interval < 0 )
    {
      fprintf( stderr, "Interval must be great than 0\n" );
      return 1;
    }
  if( options.interval == 0 )
    {
      options.interval = 3;
    }
  return 0;
}

int process_options( int argc, char ** argv )
{
  options.debug = 0;
  options.uid = 0;
  options.un = 0;
  options.interval = 0;
  options.report_cpu_hogs = 0;
  options.cpu_hog_threshold = 10;
  if( argp_parse( &argp, argc, argv, ARGP_IN_ORDER, NULL, NULL ) == 0 )
    {
      return check_options();
    }
  return 10;
}



